package cn.edu.hubu.javaparkinglot.service.impl;

import cn.edu.hubu.javaparkinglot.VO.ResultVO;
import cn.edu.hubu.javaparkinglot.entity.ParkingInfo;
import cn.edu.hubu.javaparkinglot.mapper.ParkingMapper;
import cn.edu.hubu.javaparkinglot.service.ParkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ParkingServiceImpl implements ParkingService {

    @Autowired
    private ParkingMapper parkingMapper; // 注入 ParkingMapper 对象

    @Override
    public ResultVO getParkingById(int parkId) {
        // 1. 调用 ParkingMapper 的 selectByParkId 方法查询停车场信息
        ParkingInfo parkingInfo = parkingMapper.selectByParkId(parkId);

        // 2. 根据查询结果返回响应
        if (parkingInfo != null) {
            return new ResultVO(200, "查询成功", parkingInfo);
        } else {
            return new ResultVO(500, "停车场不存在", null);
        }
    }

    @Override
    public ResultVO saveParking(ParkingInfo parkingInfo) {
        // 1. 调用 ParkingMapper 的 insertParking 方法保存停车场信息
        int result = parkingMapper.insertParking(parkingInfo);

        // 2. 根据结果返回响应
        if (result > 0) {
            return new ResultVO(200, "添加成功", parkingInfo);
        } else {
            return new ResultVO(500, "添加失败", null);
        }
    }

    @Override
    public ResultVO deleteParking(int parkId) {
        // 1. 调用 ParkingMapper 的 deleteParkingById 方法删除停车场信息
        int result = parkingMapper.deleteParkingById(parkId);

        // 2. 根据结果返回响应
        if (result > 0) {
            return new ResultVO(200, "删除成功", null);
        } else {
            return new ResultVO(500, "删除失败或停车场不存在", null);
        }
    }

    @Override
    public ResultVO updateParking(ParkingInfo parkingInfo) {
        // 1. 调用 ParkingMapper 的 updateParkingById 方法更新停车场信息
        int result = parkingMapper.updateParkingById(parkingInfo);

        // 2. 根据结果返回响应
        if (result > 0) {
            return new ResultVO(200, "修改成功", parkingInfo);
        } else {
            return new ResultVO(500, "修改失败或停车场不存在", null);
        }
    }
    @Override
    public ResultVO getAllParkings() {
        List<ParkingInfo> parkingInfos = parkingMapper.findAllParkings();
        if (parkingInfos!= null &&!parkingInfos.isEmpty()) {
            return new ResultVO(200, "查询成功", parkingInfos);
        } else {
            return new ResultVO(500, "未查询到停车场信息", null);
        }
    }
}